;
(function () {
	new Vue({
		el: '#finicial_books_config',
		data: {
			accountingArr: [],
			accountingSelectId: '',
			projectArr: [],
			projectSelect: [],
			projectSelectIds: [],
			finName: '',
			addFlag: false,
			editFlag: false,
			detail: {
				projects: []
			},
			ops: {
				bar: {
					background: '#c1c1c1'
				}
			},
			options: [{
				name: '是',
				code: 'Y'
			}, {
				name: '否',
				code: 'N'
			}],
			pushFlag: [],
			financeSoftwareVersion: ['金蝶EAS7.5.0'],
			pushAddress: '',
			companyName: '',
			companyCode: '',
			pushUserName: '',
			pushPassword: '',
			dataCenter: '',
			documentingUserName: '',
		},
		methods: {
			//获取账套列表
			accountingsDt() {
				hlhttp.get({
					url: 'acct/accountings',
					success: (data) => {

						this.accountingArr = data;
						if (!data.length) {
							this.detail = {
								projects: []
							}
							this.projectSelect = []
							hlUrlUtil.addURLParam({
								isTab: false
							})
							return
						}
						if (this.addFlag) {
							this.accountingSelectId = data[data.length - 1].id;
						}
						if (!this.addFlag && !sessionStorage.getItem("accountId")) {
							this.accountingSelectId = data[0].id;
							sessionStorage.setItem("accountId", this.accountingSelectId);
						}
						if (!this.addFlag && sessionStorage.getItem("accountId")) {
							this.accountingSelectId = sessionStorage.getItem("accountId")
						}
						this.accountingDt()
						this.addFlag = false;
						hlUrlUtil.addURLParam({
							isTab: true
						})
					}
				})
			},
			//获取可配置账套的项目列表
			configProjectsDt() {
				hlhttp.get({
					url: 'acct/accounting/config_projects',
					success: (data) => {
						this.projectArr = data;
					}
				})
			},
			//获取账套详情
			accountingDt() {
				hlhttp.get({
					url: 'acct/accounting',
					data: {
						id: this.accountingSelectId
					},
					success: (data) => {
						this.detail = data;
						this.projectSelect = []
						this.pushFlag = [data.pushFlag]
						this.financeSoftwareVersion = data.financeSoftwareVersion ? [data.financeSoftwareVersion] : ['金蝶EAS7.5.0']
						this.pushAddress = data.pushAddress
						this.companyCode = data.companyCode
						this.companyName = data.companyName
						this.pushUserName = data.pushUserName
						this.pushPassword = data.pushPassword
						this.dataCenter = data.dataCenter
						this.documentingUserName = data.documentingUserName
						data.projects.map(item => {
							this.projectSelect.push({
								projectId: item.projectId,
								projectName: item.projecName
							})
						})
					}
				})
			},
			//选择账套
			selectAccounting(item) {
				if (this.addFlag) return;
				this.accountingSelectId = item.id;
				sessionStorage.setItem("accountId", this.accountingSelectId);
				this.accountingDt()
			},
			//删除账套
			del(item) {
				hllayer.confirm('账套删除后，<br/>已配置项目凭证规则信息将全部清除且不可恢复', () => {
					hlhttp.delete({
						url: 'acct/accounting?id=' + item.id,
						success: () => {
							hl.toast('删除' + item.name + '成功')
							this.accountingsDt()
						}
					})
				})
			},
			//编辑数据纠正
			edit() {
				this.editFlag = true;
				this.projectSelect = [];
				this.projectSelectIds = [];
				this.addFlag = true;
				this.pushPassword = ''
				this.configProjectsDt()
				let data = JSON.parse(JSON.stringify(this.detail));
				this.finName = data.name;
				data.projects.map(item => {
					this.projectSelect.push({
						projectId: item.projectId,
						projectName: item.projecName
					})
					this.projectSelectIds.push(item.projectId)
				})
			},
			//选择项目
			selectProject(item, i) {
				this.projectSelectIds.push(item.projectId)
				this.projectSelect.push(item)
				this.projectArr.splice(i, 1)
			},
			//删除项目
			delProject(item, i) {
				if (!this.addFlag) return;
				this.projectArr.push(item)
				this.projectSelectIds.splice(i, 1)
				this.projectSelect.splice(i, 1)
			},
			//新增
			add() {
				this.finName = '';
				this.projectSelect = [];
				this.projectSelectIds = [];
				this.pushFlag = []
				this.financeSoftwareVersion = ['金蝶EAS7.5.0']
				this.pushAddress = ''
				this.companyName = ''
				this.companyCode = ''
				this.pushUserName = ''
				this.pushPassword = ''
				this.dataCenter = ''
				this.documentingUserName = ''
				this.addFlag = true;
				this.editFlag = false;
				this.configProjectsDt()
			},
			//保存
			submitSave() {
				let validate = new HlValidator();
				validate.add(this.finName, [{
					rule: 'isEmpty',
					msg: '账套名称不能为空'
				}, ])
				var errorMsg = validate.start()
				if (errorMsg != true) {
					hl.toast(errorMsg)
					return
				}
				if (!this.projectSelect.length) {
					hl.toast('请选择可配置项目')
					return
				}
				if (!this.pushFlag.length) {
					hl.toast('请选择是否推送')
					return
				}
				if (this.pushFlag + '' == 'Y') {
					if (!this.pushAddress) {
						hl.toast('请输入推送地址')
						return
					}
					if (!this.companyCode) {
						hl.toast('请输入项目公司编码')
						return
					}
					if (!this.companyName) {
						hl.toast('请输入项目公司名称')
						return
					}
					if (!this.pushUserName) {
						hl.toast('请输入登录用户名')
						return
					}
					if (!this.editFlag && !this.pushPassword) {
						hl.toast('请输入登录密码')
						return
					}
					if (!this.dataCenter) {
						hl.toast('请输入数据中心')
						return
					}
					if (!this.documentingUserName) {
						hl.toast('请输入制单人')
						return
					}
				}
				let projectList = [];
				this.projectSelect.map(item => {
					projectList.push({
						project_id: item.projectId,
						project_name: item.projectName
					})
				})

				let data = {
					name: this.finName,
					project_info: JSON.stringify(projectList),
					push_flag: this.pushFlag + '',
					finance_software_version: this.financeSoftwareVersion + '',
					push_address: this.pushAddress || '#',
					company_name: this.companyName || '#',
					company_code: this.companyCode || '#',
					push_user_name: this.pushUserName || '#',
					push_password: this.pushPassword || '#',
					data_center: this.dataCenter || '#',
					documenting_user_name: this.documentingUserName || '#'
				}
				//新增
				if (!this.editFlag) {
					hlhttp.post({
						url: 'acct/accounting',
						data: data,
						success: () => {
							this.accountingsDt()
							this.finName = ''
							this.projectSelect = [];
							this.projectSelectIds = [];
							hl.toast('新增成功')
						}
					})
				}
				//编辑
				if (this.editFlag) {
					data.id = this.detail.id
					hlhttp.put({
						url: 'acct/accounting',
						data: data,
						success: () => {
							this.addFlag = false;
							this.detail.name = JSON.parse(JSON.stringify(this.finName))
							this.accountingArr.map(item => {
								if (item.id == this.accountingSelectId) {
									item.name = JSON.parse(JSON.stringify(this.finName))
								}
							})
							hl.toast('编辑成功')
						}
					})
				}
			}
		},
		mounted() {
			this.accountingsDt()
		}
	})
}())